package com.yc.txx.controller;

import com.yc.txx.service.BizException;
import com.yc.txx.vo.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/5/14
 * Email haijunzhou@hnit.edu.cn
 */
@Component
@RestControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(BizException.class)
    Result bizException(BizException e){
        return Result.fail(e.getMessage(), e.getData());
    }


    // 拦截 除 BizException 以外的异常
    @ExceptionHandler(Exception.class)
    Result exception(Exception e){
        e.printStackTrace();
        return Result.fail("业务繁忙,请稍后再试!");
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    Result maxUploadSizeExceededException(MaxUploadSizeExceededException e){
        System.out.println("大小e.getMaxUploadSize() = " + e.getMaxUploadSize());
        return Result.fail("上传的文件大小不能超过 1 MB!");
    }
}
